package com.snow4cloud.common.datasource.config;

import com.baomidou.dynamic.datasource.provider.DynamicDataSourceProvider;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceAutoConfiguration;
import com.snow4cloud.common.datasource.property.DataSourceProperties;
import lombok.AllArgsConstructor;
import org.jasypt.encryption.StringEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 动态数据源自动配置
 *
 * @Description
 * @Author fanxl
 * @Date 2021/5/6
 **/
@AllArgsConstructor
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
@AutoConfigureBefore(DynamicDataSourceAutoConfiguration.class)
@EnableConfigurationProperties(DataSourceProperties.class)
public class SnowDynamicDataSourceAutoConfiguration {

    private  DataSourceProperties properties;

    private  StringEncryptor stringEncryptor;

    @Bean
    @ConditionalOnMissingBean(DynamicDataSourceProvider.class)
    public DynamicDataSourceProvider dynamicDataSourceProvider() {
        return new JdbcDynamicDataSourceProvider(stringEncryptor, properties);
    }

}
